package com.fearless.crm.workbench.service.impl;

import com.fearless.crm.settings.dao.UserDao;
import com.fearless.crm.settings.entity.User;
import com.fearless.crm.utils.DateTimeUtil;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.vo.ClueVoPagination;
import com.fearless.crm.workbench.dao.*;
import com.fearless.crm.workbench.entity.*;
import com.fearless.crm.workbench.service.ClueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ClueServiceImpl implements ClueService {

    @Resource
    private UserDao userDao;
    @Resource
    private ActivityDao activityDao;

//线索相关的dao
    @Resource
    private ClueActivityRelationDao clueActivityRelationDao;
    @Resource
    private ClueDao clueDao;
    @Resource
    private ClueRemarkDao clueRemarkDao;

//客户相关的dao
    @Resource
    private CustomerDao customerDao;
    @Resource
    private CustomerRemarkDao customerRemarkDao;

//联系人相关的dao
    @Resource
    private ContactsDao contactsDao;
    @Resource
    private ContactsRemarkDao contactsRemarkDao;
    @Resource
    private ContactsActivityRelationDao contactsActivityRelationDao;

//交易相关的dao
    @Resource
    private TranDao tranDao;
    @Resource
    private TranHistoryDao tranHistoryDao;







//获取用户列表，用于铺垫创建线索的模态窗口,业务层方法
    public List<User> getUserListService() {
        List<User> userList=userDao.getUserListDao();
        return userList;
    }

//执行线索的创建操作，业务层方法
    public boolean saveClueService(Clue clue) {
        boolean flag=true;
        int count=clueDao.saveClueDao(clue);
        if(count!=1){
            flag=false;
        }
        return flag;
    }


//分页查询,业务层方法
    public ClueVoPagination pageListService(Map map) {
    //业务层处理流程
    // 1.通过dao取得total
    //2.通过dao取得datalist
    //3.将total和datalist封装到vo
    //将vo返回给处理器方法
        int total=clueDao.getTotalByCondition(map);
        List<Clue> datalist=clueDao.getClueListByCondition(map);
        ClueVoPagination clueVoPagination=new ClueVoPagination();
        clueVoPagination.setTotal(total);
        clueVoPagination.setDatalist(datalist);

        return clueVoPagination;
    }


//获取线索的详细信息，用于铺垫线索的详细信息页,业务层方法
    public Clue getClueDetailService(String id) {

        Clue clue=clueDao.getClueDetailDao(id);
        return clue;
    }


//获取线索关联的市场活动列表
    public List<Activity> getActivityByClueIdService(String clueId) {

        List<Activity> activityList=activityDao.getActivityListByClueId(clueId);
        return activityList;
    }



//解除线索关联的市场活动
    public Map unBundClueWithActivityService(String id) {
        boolean flag=true;
        int count=clueActivityRelationDao.unBundClueWithActivityDao(id);
        if(count!=1){
            flag=false;
        }
        Map map=new HashMap();
        map.put("success",flag);
        return map;
    }



//获取线索的详细信息，用于铺垫修改线索的模态窗口,业务层方法
    public Map<String,Object> editClueService(String id) {
        Clue clue=clueDao.editClueDao(id);
        List<User> userList=userDao.getUserListDao();
        Map map=new HashMap();
        map.put("userList",userList);
        map.put("clue",clue);
        return map;
    }


//执行关系表的添加操作，就是将线索和市场活动关联起来   【批量添加】,业务层方法
    public boolean connectService(String clueId, String[] activityId) {
        boolean flag=true;
        //clueActivityRelationDao.connectDao();
            for(String aid:activityId){
                //遍历数组中的市场活动id,然后将每一个市场活动id与clueId关联，
                ClueActivityRelation  car=new ClueActivityRelation();
                car.setId(UUIDUtil.getUUID());
                car.setActivityId(aid);
                car.setClueId(clueId);
                int count=clueActivityRelationDao.connectDao(car);
                if (count!=1){
                    flag=false;
                }
            }



        return flag;
    }

//执行线索的修改操作，业务层方法
    public boolean updateClueService(Clue clue) {
        boolean flag=true;
        int count=clueDao.updateClueDao(clue);
        if(count!=1){
            flag=false;
        }

        return flag;
    }

//执行线索的删除操作，业务层方法
    public boolean deleteClueByIdService(String[] id) {
        int count=clueDao.deleteClueByIdDao(id);
        boolean flag=true;
        if(count!=id.length){

            flag=false;
        }

        return flag;
    }

//在详细信息页修改线索,业务层方法
    public boolean editClueInDetailService(Clue clue) {
        boolean flag=true;

        int count=clueDao.editClueInDetailDao(clue);
        if(count!=1){
            flag=false;
        }


        return flag;
    }



//在详细页执行线索的删除操作,业务层方法
    public boolean deleteClueInDetailService(String id) {
        boolean flag=true;

        int count=clueDao.deleteClueInDetailDao(id);
        if(count!=1){
            flag=false;
        }


        return flag;
    }




//线索转换，业务层方法
    public boolean convertService(String clueId, Tran tran,String createBy) {
        System.out.println("进入到convertService方法");
         boolean flag=true;
         String createTime= DateTimeUtil.getSysTime();


     //1.通过线索id获取线索对象(线索对象中封装了线索的信息)
        Clue clue=clueDao.getClueById(clueId);

     //2.通过线索对象提取客户信息,当该客户不存在时,新建客户(根据公司名称来精确匹配,从而判断客户是否存在)
        String company=clue.getCompany();

        //客户表中的name字段就是线索表中的company字段
        Customer customer= customerDao.getCustomerByName(company);
        //如果customer为空，说明之前没有这个客户，那么我们需要新建一个
        if(customer==null){
            customer=new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setCreateBy(createBy);
            customer.setCreateTime(createTime);
            //线索转换,把线索中的信息转换为客户表中的信息(这是把线索中与公司有关的信息转到客户表)
            //一会还需要把线索中与人有关的信息转到联系人表 . 线索===潜在客户
            //联系人表以及客户表===真实客户  我们现在做是将潜在客户变为真实客户
            customer.setAddress(clue.getAddress());//公司地址
            customer.setName(clue.getCompany());//公司名称
            customer.setDescription(clue.getDescription());//线索描述
            customer.setPhone(clue.getPhone());//公司电话
            customer.setWebsite(clue.getWebsite());//公司网站
            customer.setNextContactTime(clue.getNextContactTime());//下次联系时间
            customer.setContactSummary(clue.getContactSummary());//联系概要
            customer.setOwner(clue.getOwner());//所有者

            //添加客户
            int count=customerDao.saveNerCustomer(customer);

            if(count!=1){
                flag=false;
            }
        }
        //进过第二步的处理,客户的信息我们拥有了,将来在处理其他表的时候,可以提供客户表的id


     //3.通过线索对象提取联系人，保存联系人. 线索转换为联系人
        Contacts contacts=new Contacts();

        contacts.setId(UUIDUtil.getUUID());
        contacts.setAddress(clue.getAddress());
        contacts.setAppellation(clue.getAppellation());
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setCreateBy(createBy);
        contacts.setCreateTime(createTime);
        contacts.setDescription(clue.getDescription());
        contacts.setFullname(clue.getFullname());
        contacts.setEmail(clue.getEmail());
        contacts.setJob(clue.getJob());
        contacts.setMphone(clue.getMphone());
        contacts.setOwner(clue.getOwner());
        contacts.setSource(clue.getSource());
        contacts.setCustomerId(customer.getId());//客户表的id，就是公司的id
        //添加联系人
         int count2= contactsDao.saveNewContact(contacts);
        if(count2!=1){
            flag=false;
        }
         //进过第三步的处理,联系人的信息我们拥有了,将来在处理其他表的时候,可以提供联系人表的id


     //4.【线索备注】转换到【客户备注】以及联系人备注
        //查询出与该线索关联的备注信息列表
        List<ClueRemark> clueRemarkList=clueRemarkDao.getClueRemark(clueId);
        //取出每一条线索相关的备注
        for(ClueRemark clueRemark:clueRemarkList){
            //取出每一条线索相关的备注信息
            String noteContent=clueRemark.getNoteContent();

            //创建客户备注对象，添加客户备注
            CustomerRemark customerRemark=new CustomerRemark();
            customerRemark.setId(UUIDUtil.getUUID());
            customerRemark.setCreateBy(createBy);
            customerRemark.setCreateTime(createTime);
            customerRemark.setCustomerId(customer.getId());
            customerRemark.setNoteContent(noteContent);
            customerRemark.setEditFlag("0");
            int count3=customerRemarkDao.saveCustomerRemarkService(customerRemark);
            if (count3!=1){
                flag=false;
            }

            //创建联系人备注对象,
            ContactsRemark contactsRemark=new ContactsRemark();
            contactsRemark.setId(UUIDUtil.getUUID());
            contactsRemark.setCreateBy(createBy);
            contactsRemark.setCreateTime(createTime);
            contactsRemark.setEditFlag("0");
            contactsRemark.setNoteContent(noteContent);
            contactsRemark.setContactsId(contacts.getId());
            int count4=contactsRemarkDao.saveContactsRemarkService(contactsRemark);
            if(count4!=1){
                flag=false;
            }

        }


     //5.将【线索和市场活动的关系】转为【联系人和市场活动的关系】
        //找出与此条线索关联的市场活动，查询线索与市场活动关联关系表
        List<ClueActivityRelation> clueActivityRelationList=clueActivityRelationDao.getListByClueId(clueId);
        //遍历出每一条与市场活动关联的关联关系记录
        for(ClueActivityRelation clueActivityRelation:clueActivityRelationList){
            //从每一条遍历出来的记录中取出关联的市场活动id
            String activityId=clueActivityRelation.getActivityId();

            //创建【联系人】与【市场活动】的关联关系对象，让第三步生成的联系人与市场活动做关联
            ContactsActivityRelation contactsActivityRelation=new ContactsActivityRelation();
            contactsActivityRelation.setId(UUIDUtil.getUUID());
            contactsActivityRelation.setActivityId(activityId);
            contactsActivityRelation.setContactsId(contacts.getId());
            //添加【联系人】与【市场活动】的关联关系
            int count5=contactsActivityRelationDao.saveRelation(contactsActivityRelation);
            if(count5!=1){
                flag=false;
            }
        }

    //6.如果有创建交易的请求，我们就创建交易
        if(tran!=null){//说明用户需要创建交易对象
            /*
                tran(交易)对象已经在controller封装好了一些主要的信息
                接下来可以通过第一步生成的clue对象取出一些信息，去继续完善对tran的封装
             */
            tran.setSource(clue.getSource());
            tran.setOwner(clue.getOwner());
            tran.setNextContactTime(clue.getNextContactTime());
            tran.setDescription(clue.getDescription());
            tran.setCustomerId(customer.getId());
            tran.setContactsId(contacts.getId());
            tran.setContactSummary(clue.getContactSummary());
            //添加交易
            int count6=tranDao.saveTran(tran);
            if(count6!=1){
                flag=false;
            }


    //7.如果创建了交易，则创建一条该交易下的【交易历史】
            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtil.getUUID());
            tranHistory.setCreateBy(createBy);
            tranHistory.setCreateTime(createTime);
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setStage(tran.getStage());
            tranHistory.setTranId(tran.getId());

        //创建交易历史
            int count7=tranHistoryDao.saveTranHistory(tranHistory);
            if(count7!=1){
                flag=false;
            }


        }
    //8.删除线索的备注
       for(ClueRemark clueRemark:clueRemarkList){
            int count8=clueRemarkDao.deleteClueRemarkConvertDao(clueRemark);
            if(count8!=1){
                flag=false;
            }
        }



    //9.删除线索与市场活动关联
        for(ClueActivityRelation clueActivityRelation:clueActivityRelationList){

           int count9= clueActivityRelationDao.deleteClueActivityRelation(clueActivityRelation);
           if(count9!=1){
               flag=false;
           }

        }



    //10.删除线索
        int count10=clueDao.deleteClueByClueIdDao(clueId);
        if(count10!=1){
            flag=false;
        }


        return flag;
    }




}
